<?php

namespace Addons\OaSystem\Model;
use Think\Model;

/**
 * OaSystem模型
 */
class OaUserEmployeeModel extends Model{

    public function addUserEmployee($data)
    {
        if ($data == NULL){
            return null;
        }
        $map['appid'] = $data['appid'];
        $map ['emp_id'] = $data['emp_id'];
        // var_dump($data,$map);
        $select_data = $this->where($map)->select();
        //var_dump($select_data);
       // return;
        if ($select_data != NULL) {
            //删除没有了的电话
            foreach($select_data as $dbItem) {
                if(!in_array($dbItem['mobile'], $data['mobile_array'])) {
                    $map['mobile'] = $dbItem['mobile'];
                    $this->where($map)->delete();
                }
            }

            //添加新增的电话
            foreach($data['mobile_array'] as $mobile) {
                $find = false;
                foreach($select_data as $dbItem) {
                    if($mobile == $dbItem['mobile']) {
                        $find = true;
                        break;
                    }
                }
                if(!$find) {
                    $data['mobile'] = $mobile;
                    $data['create_time'] = $data['time'];
                    $res = $this->add ( $data );
                }
            }
            return $res;
        } else {
            $data['create_time'] = $data['time'];
            foreach($data['mobile_array'] as $mobile) {
                $data['mobile'] = $mobile;
                $res = $this->add ( $data );
            }
            return $res;
        }
    }

    public function update($data){
        if ($data == NULL){
            return null;
        }
        $map ['id'] = $data['id'];

        $res = $this->where($map)->save($data);
        return $res;
    }
}
